import os
from openpyxl import Workbook

"""
将指定文件夹下的所有子目录写入Excel文件
"""
def list_folders_to_excel(folder_path, excel_file):
    # 初始化工作簿
    wb = Workbook()
    ws = wb.active
    ws.title = "Folders"

    # 写入表头
    ws["A1"] = "新文件夹名称"
    ws["B1"] = "Absolute Path"
    ws["C1"] = "借据单号"

    # 遍历指定路径下的所有子目录
    row_num = 2  # 从第二行开始写入数据
    for root, dirs, files in os.walk(folder_path):
        for dir_name in dirs:
            absolute_path = os.path.join(root, dir_name)
            ws.cell(row=row_num, column=1, value=dir_name)
            ws.cell(row=row_num, column=2, value=absolute_path)
            ws.cell(row=row_num, column=3, value=dir_name)
            row_num += 1

    # 保存到Excel文件
    wb.save(excel_file)
    print(f"Folder information has been saved to {excel_file}")

if __name__ == "__main__":
    folder_path = "D:\\Documents\\WeChat Files\\huangkeweiaini\\FileStorage\\File\\2024-08\\济南5笔样本"
    excel_file = "D:\\Documents\\WeChat Files\\huangkeweiaini\\FileStorage\\File\\2024-08\\济南5笔样本\\1.xlsx"
    list_folders_to_excel(folder_path, excel_file)
